package com.macro.mall.portal.service.impl;

import com.macro.mall.mapper.ContentTagsMapper;
import com.macro.mall.mapper.EvaluateMapper;
import com.macro.mall.mapper.EvaluateVideoPhotoMapper;
import com.macro.mall.model.EvaluateAddRequest;
import com.macro.mall.model.EvaluateRes;
import com.macro.mall.model.EvaluateResponse;
import com.macro.mall.model.EvaluateVideoPhoto;
import com.macro.mall.portal.service.EvaluateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @Author：尚志豪
 * @Package：com.macro.mall.portal.service.impl
 * @Project：mall-swarm-B
 * @name：EvaluateServiceImpl
 * @Date：2024/10/31 17:29
 */
@Service
public class EvaluateServiceImpl implements EvaluateService {
    @Autowired
    private EvaluateMapper evaluateMapper;

    @Autowired
    private ContentTagsMapper contentTagsMapper;

    @Autowired
    private EvaluateVideoPhotoMapper evaluateVideoPhotoMapper;

    @Autowired
    private ApplicationContext applicationContext;



    @Override
    @Transactional
    public void save(EvaluateAddRequest evaluateAddRequest) {
        long start = System.currentTimeMillis();
        evaluateMapper.save(evaluateAddRequest.getEvaluate());
        EvaluateServiceImpl bean = applicationContext.getBean(EvaluateServiceImpl.class);

        //添加到标签中间表
        if (!CollectionUtils.isEmpty(evaluateAddRequest.getTagIds())){
            bean.save(evaluateAddRequest.getTagIds(),evaluateAddRequest.getEvaluate().getId());
        }
        //添加视图表
        if (evaluateAddRequest.getEvaluate().getId()!=null){
            EvaluateVideoPhoto evaluateVideoPhoto = new EvaluateVideoPhoto();
            evaluateVideoPhoto.setContentId(evaluateAddRequest.getEvaluate().getId());
            bean.savePhoto(evaluateVideoPhoto);
        }
        long end = System.currentTimeMillis();
        System.out.println("运行时间，耗时：" + (end - start) + "毫秒");
    }

    @Async("AsyncExecutor")
    public void save(List<Integer> ids,Integer id){
        System.out.println("内部线程：" + Thread.currentThread().getName());
        long start = System.currentTimeMillis();
        contentTagsMapper.save(ids,id);
        //Thread Sleep
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        long end = System.currentTimeMillis();
        System.out.println("完成标签评价中间表，耗时：" + (end - start) + "毫秒");
    }

    @Async("AsyncExecutor")
    public void savePhoto(EvaluateVideoPhoto evaluateVideoPhoto){
        System.out.println("内部线程：" + Thread.currentThread().getName());
        long start = System.currentTimeMillis();
        evaluateVideoPhotoMapper.savePhoto(evaluateVideoPhoto);

        //Thread Sleep
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        long end = System.currentTimeMillis();
        System.out.println("完成添加图片评价中间表，耗时：" + (end - start) + "毫秒");
    }


    @Override
    public int getNum(Integer productId) {
        return evaluateMapper.getNum(productId);
    }

    @Override
    public EvaluateRes list(Integer productId) {
        List<EvaluateResponse> list = evaluateMapper.list(productId);
        int num = evaluateMapper.getNum(productId);
        int a = 0;
        for (EvaluateResponse evaluateResponse : list) {
            //如果评分大于35算好评
            if(evaluateResponse.getCourierLevel()+evaluateResponse.getExpressageLevel()+evaluateResponse.getSkuLevel()+evaluateResponse.getSpeedLevel()>35){
                a+=1;
            }
        }
        EvaluateRes evaluateRes = new EvaluateRes(list, num, (a * 1.0 / num) * 100 + "%");

        return evaluateRes;
    }

    @Override
    public EvaluateResponse getById(Integer id) {
        return evaluateMapper.getById(id);
    }



    //3.
    // 商品的评论列表查询也需迭代，
    // 不允许使用多表联查，需单独查询评论列表、
    // 根据评论id查询标签、根据评论id查询视图，并且每个查询方法都迭代为异步，测试异步与非异步的区别
    @Override
    public EvaluateRes findList(Integer productId) {
        //通过

        //todo

        return null;
    }
}
